﻿using System;
using System.Collections.Generic;

// 定义形状接口
interface IShape
{
    double CalculateArea(); // 计算面积
}

// 长方形类实现形状接口
class Rectangle : IShape
{
    private double length;
    private double width;

    public Rectangle(double length, double width)
    {
        this.length = length;
        this.width = width;
    }

    public double CalculateArea()
    {
        return length * width;
    }
}

// 正方形类实现形状接口
class Square : IShape
{
    private double side;

    public Square(double side)
    {
        this.side = side;
    }

    public double CalculateArea()
    {
        return side * side;
    }
}

// 三角形类实现形状接口
class Triangle : IShape
{
    private double side1;
    private double side2;
    private double side3;

    public Triangle(double side1, double side2, double side3)
    {
        this.side1 = side1;
        this.side2 = side2;
        this.side3 = side3;
    }

    public double CalculateArea()
    {
        // 使用海伦公式计算三角形面积
        double s = (side1 + side2 + side3) / 2;
        double area = Math.Sqrt(s * (s - side1) * (s - side2) * (s - side3));
        return area;
    }
}

// 形状工厂类
class ShapeFactory
{
    private Random random;

    public ShapeFactory()
    {
        random = new Random();
    }

    // 随机创建一个形状对象
    public IShape CreateShape()
    {
        int shapeType = random.Next(1, 4); // 随机生成1~3的整数

        switch (shapeType)
        {
            case 1:
                double length = random.NextDouble() * 10 + 1; // 随机生成1~11之间的浮点数
                double width = random.NextDouble() * 10 + 1;
                return new Rectangle(length, width);
            case 2:
                double side = random.NextDouble() * 10 + 1;
                return new Square(side);
            case 3:
                double side1 = random.NextDouble() * 10 + 1;
                double side2 = random.NextDouble() * 10 + 1;
                double side3 = random.NextDouble() * 10 + 1;
                return new Triangle(side1, side2, side3);
            default:
                return null;
        }
    }
}

class Program
{
    static void Main()
    {
        ShapeFactory shapeFactory = new ShapeFactory();

        double totalArea = 0;

        for (int i = 0; i < 10; i++)
        {
            IShape shape = shapeFactory.CreateShape();
            if (shape != null)
            {
                double area = shape.CalculateArea();
                totalArea += area;
                Console.WriteLine("形状" + (i + 1) + "的面积为：" + area);
            }
        }

        Console.WriteLine("这10个形状的总面积为：" + totalArea);

        Console.ReadLine();
    }
}